package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.BooksInformation;
import com.example.demo.entity.requestJson.ABookInformationReq;
import com.example.demo.entity.requestJson.AUpdateBookInfoReq;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.responseJson.ABookBaseInfoRes;
import com.example.demo.entity.responseJson.AQueryBookRes;
import com.example.demo.entity.responseJson.ListResult;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.BooksInformationMapper;
import com.example.demo.service.AuthorCreationService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : 杨丽辉
 * @date : 2021/3/3 11:06
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class AuthorCreationServiceImpl implements AuthorCreationService {
    @Autowired
    private BooksInformationMapper informationMapper;
    @Autowired
    private AuthorMapper authorMapper;

    /**
    * 新作品开坑填写书籍基本信息
    * */
    @Override
    public BackResponseEntity<Object> insertInformation(ABookInformationReq aBookInformationReq){
        String bookName = null;
        String info = null;
        String id = null;
        String pic = null;
        String onelabel = null;
        String twolabel = null;
        Integer attribute = null;
        String oneWord = null;

        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookName())){
            bookName = aBookInformationReq.getBookName()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookInfo())){
            info = aBookInformationReq.getBookInfo()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getAuthorId())){
            id = aBookInformationReq.getAuthorId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookPic())){
            pic = aBookInformationReq.getBookPic()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookOnelabel())){
            onelabel = aBookInformationReq.getBookOnelabel()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookTwolabel())){
            twolabel = aBookInformationReq.getBookTwolabel()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getBookAttributes())){
            attribute = aBookInformationReq.getBookAttributes();
        }
        if(!ObjectUtils.isEmpty(aBookInformationReq.getOneWord())){
            oneWord = aBookInformationReq.getOneWord()
                    .trim();
        }
        if(ObjectUtils.isEmpty(bookName) || ObjectUtils.isEmpty(info) || ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(attribute)
         || ObjectUtils.isEmpty(onelabel) || ObjectUtils.isEmpty(twolabel)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }
        if(ObjectUtils.isEmpty(pic)){
            pic = "http://i-1.wzsky.net/2020/9/9/W3dtOnN5LnBuZyxyOjEzLGI6MTNd/971227e1-de75-407d-be91-25c30c36bb9e.jpg";
        }

        Author author = authorMapper.selectById(id);
        BooksInformation booksInformation = new BooksInformation();
        booksInformation.setBookName(bookName);
        booksInformation.setBookInfo(info);
        booksInformation.setAuthorId(id);
        booksInformation.setBookStatus(2);
        booksInformation.setBookPic(pic);
        booksInformation.setBookAttributes(attribute);
        booksInformation.setBookOnelabel(onelabel);
        booksInformation.setBookTwolabel(twolabel);
        booksInformation.setBookStartTime(LocalDateTime.now());
        booksInformation.setContentCount(0);
        booksInformation.setFavoritesCount(0);
        booksInformation.setCommentCount(0);
        booksInformation.setOneWord(oneWord);
        booksInformation.setAuthorName(author.getAuthorNickname());

        int i = informationMapper.insert(booksInformation);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "新作品开坑成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1010, "新作品开坑失败");
        }
    }

   /**
   编辑书籍信息
    */
    @Override
    public BackResponseEntity<Object> updateInformation(AUpdateBookInfoReq aUpdateBookInfoReq){
        String bookName = null;
        String info = null;
        Integer status = null;
        String id = null;
        String pic = null;
        String onelabel = null;
        String twolabel = null;
        Integer attribute = null;
        String oneWord = null;

        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookName())){
            bookName = aUpdateBookInfoReq.getBookName()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookInfo())){
            info = aUpdateBookInfoReq.getBookInfo()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBooksId())){
            id = aUpdateBookInfoReq.getBooksId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookPic())){
            pic = aUpdateBookInfoReq.getBookPic()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookOnelabel())){
            onelabel = aUpdateBookInfoReq.getBookOnelabel()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookTwolabel())){
            twolabel = aUpdateBookInfoReq.getBookTwolabel()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookAttributes())){
            attribute = aUpdateBookInfoReq.getBookAttributes();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getBookStatus())){
            status = aUpdateBookInfoReq.getBookStatus();
        }
        if(!ObjectUtils.isEmpty(aUpdateBookInfoReq.getOneWord())){
            oneWord = aUpdateBookInfoReq.getOneWord()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LocalDateTime time = LocalDateTime.now();
        LambdaUpdateWrapper<BooksInformation> wrappers = new UpdateWrapper<BooksInformation>().lambda();

        wrappers.eq(!ObjectUtils.isEmpty(id), BooksInformation::getBooksId, id)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(bookName),
                        BooksInformation::getBookName, bookName)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(info),
                        BooksInformation::getBookInfo, info)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(status),
                        BooksInformation::getBookStatus, status)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(pic),
                        BooksInformation::getBookPic, pic)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(attribute),
                        BooksInformation::getBookAttributes, attribute)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(onelabel),
                        BooksInformation::getBookOnelabel, onelabel)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(twolabel),
                        BooksInformation::getBookTwolabel, twolabel)
                .set(!ObjectUtils.isEmpty(id) && !ObjectUtils.isEmpty(oneWord),
                        BooksInformation::getOneWord, oneWord)
                .set(!ObjectUtils.isEmpty(id), BooksInformation::getBookIupdateTime, time);
        int i = informationMapper.update(null,wrappers);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "编辑作品信息成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1010, "编辑作品信息失败");
        }
    }

    @Override
    public  BackResponseEntity<Object> deleteInformation(DeleteReq deleteReq){
        String id = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            id = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        int i = informationMapper.deleteById(id);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "删除书籍成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1011, "删除书籍失败");
        }
    }

    /**
    查看作品详情
    */
    @Override
    public BackResponseEntity<AQueryBookRes> queryMoreBookInfo(DeleteReq deleteReq){
        String id = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            id = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        BooksInformation booksInformation = new BooksInformation();
        booksInformation.setBooksId(id);
        LambdaQueryWrapper<BooksInformation> queryWrapper = new QueryWrapper<BooksInformation>().lambda();
        queryWrapper.select(BooksInformation::getBooksId,BooksInformation::getAuthorId,BooksInformation::getContentCount,BooksInformation::getBookName,
                            BooksInformation::getBookPic,BooksInformation::getNewChapterUtime,BooksInformation::getBookOnelabel,BooksInformation::getBookTwolabel,
                            BooksInformation::getBookStatus,BooksInformation::getBookAttributes,BooksInformation::getNewChaptername,BooksInformation::getAuthorName)
                    .eq(!ObjectUtils.isEmpty(id),BooksInformation::getBooksId,id);
        BooksInformation booksInformation1 = new BooksInformation();
        booksInformation1 = informationMapper.selectOne(queryWrapper);

        AQueryBookRes aQueryBookRes = new AQueryBookRes();
        aQueryBookRes.setBooksId(booksInformation1.getBooksId());
        aQueryBookRes.setBookName(booksInformation1.getBookName());
        aQueryBookRes.setAuthorId(booksInformation1.getAuthorId());
        aQueryBookRes.setBookStatus(booksInformation1.getBookStatus());
        aQueryBookRes.setBookPic(booksInformation1.getBookPic());
        aQueryBookRes.setBookOnelabel(booksInformation1.getBookOnelabel());
        aQueryBookRes.setBookTwolabel(booksInformation1.getBookTwolabel());
        aQueryBookRes.setBookAttributes(booksInformation1.getBookAttributes());
        aQueryBookRes.setBookCupdateTime(booksInformation1.getNewChapterUtime());
        aQueryBookRes.setContentCount(booksInformation1.getContentCount());
        aQueryBookRes.setNewChaptername(booksInformation1.getNewChaptername());
        aQueryBookRes.setAuthorNickname(booksInformation1.getAuthorName());


        return BackResponseEntity.success(ErrCodeEnum.unknown_0, aQueryBookRes, "");

    }

    @Override
    public BackResponseEntity<ListResult<List<ABookBaseInfoRes>>> queryBaseBookInfo(DeleteReq deleteReq){
        String id = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            id = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(id)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        BooksInformation booksInformation = new BooksInformation();
        booksInformation.setBooksId(id);
        LambdaQueryWrapper<BooksInformation> queryWrapper = new QueryWrapper<BooksInformation>().lambda();
        queryWrapper.select(BooksInformation::getBooksId,BooksInformation::getAuthorId,BooksInformation::getBookName,
                BooksInformation::getBookPic,BooksInformation::getNewChapterUtime,BooksInformation::getBookIupdateTime,BooksInformation::getNewChaptername)
                .eq(!ObjectUtils.isEmpty(id),BooksInformation::getAuthorId,id);
        long count = PageHelper.count(() -> informationMapper.selectList(queryWrapper));

        List<BooksInformation> list = informationMapper.selectList(queryWrapper);
        List presenter = list;
        List<AQueryBookRes> res1 =presenter;
        ListResult<List<ABookBaseInfoRes>> listResult = new ListResult((int)count, res1);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");

    }
}
